// auth_jwt.js
const express = require('express');
const router = express.Router();
const {login} = require("../service/auth_service");
const {asyncHandler, validateRequest} = require("../tool/middleWare");

router.post('/login',
    validateRequest({
        body: {
            phoneNumber: {type: 'string', required: true},
            countryCode: {type: 'string', required: true},
            password: {type: 'string', required: true},
            type: {type: 'number', required: true, min: 0, max: 1}
        }
    }),
    asyncHandler(async (req, res) => {
        try {
            const { phoneNumber, countryCode, password, type } = req.body;
            const result = await login({ phoneNumber, countryCode, password, type });
            res.json({ success: true, data: result });
        } catch (error) {
            res.status(400).json({ success: false, message: error.message });
        }
    }));

module.exports = router;
